<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Muserecoupon extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    
    public function getEcouponUserDB($number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        $sql  = '';
        $sql .= "SELECT ecp.id, ecp.code_coupon, ecp.coupon_id, ecp.user_id, ecp.created, ecp.active, cp.title, m.username, m.fullname 
        FROM ecoupon_user AS ecp JOIN ecoupon AS cp ON ecp.coupon_id = cp.id JOIN member AS m ON ecp.user_id = m.id 
        LEFT JOIN ecoupon_key AS ek ON ek.id_ecoupon = cp.id LEFT JOIN staff AS st ON st.restaurant_id = ek.id_restaurant
		WHERE ecp.active > 0 AND ecp.user_id IS NOT NULL AND st.id = ".$this->session->userdata('admin_id')." ";
        
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= "AND (m.username like '%%".$key."%%' OR ";
            $sql .= "cp.title like like '%%".$key."%%') ";
        }
        $sql .= "ORDER BY ecp.id DESC";
        //die($sql);
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
		//echo '<pre>';die(print_r($xs));
        return array('data' => $xs, 'count' => $count);   
    }
    
    
    public function getForm($coupon)
    {
        $data = $this->db->query('SELECT id,code_coupon,coupon_id,user_id,created,admin_id,active,time_use FROM ecoupon_user WHERE code_coupon = "'.$coupon.'" ' );
		$xs = $data->row_object();
		if( $data->num_rows() == 0 ){
			return array('status' => 202, 'data' => array(), 'content' => 'Coupon khong ton tai');
		}else{
            $check_per = $this->db->query('SELECT eu.id FROM ecoupon AS cp LEFT JOIN ecoupon_user AS eu ON cp.id = eu.coupon_id LEFT JOIN ecoupon_key AS ek ON ek.id_ecoupon = cp.id WHERE eu.code_coupon = "'.$coupon.'" AND ek.id_restaurant = "'.$this->session->userdata('restaurant_id').'" ')->num_rows();
            
            if($check_per === 0 ){
                $check_per_manage = $this->db->query('SELECT r.id FROM restaurant AS r LEFT JOIN ecoupon_key AS ek ON ek.id_restaurant = r.id LEFT JOIN ecoupon_user AS eu ON eu.coupon_id = ek.id_ecoupon INNER JOIN user AS u ON u.id = r.manage_id INNER JOIN staff AS s ON s.manage_id = u.id WHERE eu.code_coupon = '.$coupon.' AND r.id = '.$this->session->userdata('restaurant_id'))->num_rows();
                if($check_per_manage === 0){
                    $xs->per = 1; //Mã coupon không thuộc chiến dịch của chuỗi/cửa hàng
                }else{
                    $xs->per = 0; //Coupon không áp dụng cho cửa hàng mà tk staff quản lý
                }
                
            }else{
                $xs->per = 2; // DONE
            }
            
          
			$coupon_id = $data->row()->coupon_id;
			$user_id = $data->row()->user_id;
            
            $ecoupon = $this->db->query('SELECT title,sale_off,`condition`,date_user FROM ecoupon WHERE id = '.$coupon_id)->row();
			$xs->title = $ecoupon->title;
			$xs->sale_off = $ecoupon->sale_off;
			$xs->condition = $ecoupon->condition;
            $xs->expire_date = $ecoupon->date_user;
            
			if( !in_array($user_id,array('',null,0))){
                $member = $this->db->query('SELECT username,fullname,mobile,active FROM member WHERE id = '.$user_id)->row();
				$xs->username = $member->username;
				$xs->fullname = $member->fullname;
				$xs->mobile = $member->mobile;
				$xs->user_active = $member->active;				
				return array('status' => 200, 'data' => $xs, 'content' => 'Chi tiet coupon');
			}else{
				return array('status' => 201, 'data' => $xs, 'content' => 'Coupon chua co nguoi nhan');
			}
			
		}
		//echo '<pre>';die(print_r($xs));
		return $xs;
    }
    
    public function selectMember($id)
    {
        $html = '';
        $sql = "SELECT id,username,fullname FROM member WHERE active = 1 ORDER BY id ASC ";
        $q = $this->db->query($sql);
        $data = $q->result_array();
        foreach($data AS $le=>$el)
        {
            if($id == $el['id'])
            {
                $selected = " selected = 'selected' ";
            }else{
                $selected = " ";
            }
            $html .= "<option value=".$el['id']." ".$selected.">".$el['username']." ---|--- ".$el['fullname']."</option>";
        }
        return $html;
    }
    
    public function addUserEcouponDB($id,$code_coupon,$active,$restaurant_id,$log_work_car,$log_bills,$log_customers,$log_revenue,$log_customer,$log_mobile,$log_gift)
    {
        $data = array( 
				'active' => $active,
				'admin_id' => $this->session->userdata('admin_id'),
				'time_use' => date('Y-m-d H:i:s')
			);
        $this->db->update('ecoupon_user', $data, "id = ".$id);
		$data_ = array ( 'created' => date('Y-m-d H:i:s'), 'restaurant_id' => $restaurant_id, 'work_car' => $log_work_car, 'bills' => $log_bills, 'customers' => $log_customers, 'revenue' => $log_revenue, 'code_coupon' => $code_coupon, 'customer' =>$log_customer, 'mobile' => $log_mobile, 'gift' => $log_gift );
		$this->db->insert('log_use_coupon',$data_);
        return 200;
    }
    
	public function listRestaurant($id,$code_coupon){
		//$data = $this->db->query('SELECT r.* FROM restaurant AS r INNER JOIN ecoupon_key AS ek ON ek.id_restaurant = r.id LEFT JOIN ecoupon AS cp ON cp.id = ek.id_ecoupon LEFT JOIN ecoupon_user AS eu ON eu.coupon_id = cp.id WHERE 1 = 1 AND eu.code_coupon = '.$code_coupon)->result_object();
        $data = $this->db->query('SELECT * FROM restaurant WHERE id = '.$this->session->userdata('restaurant_id'))->result_object();
        $html = '<option value = "">Chọn nhà hàng</option>';
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->name.'</option>';
        }
		return $html;
    }
	
	public function ajaxExport1Ecoupon($code_coupon){
		$sql = "SELECT u.fullname, eu.code_coupon, eu.active, eu.created AS time_get,lg.work_car,lg.customers,lg.revenue, lg.bills, lg.customer, lg.gift, lg.created AS time_use, r.name AS restaurant 
		FROM ecoupon_user AS eu 
		INNER JOIN log_use_coupon AS lg ON eu.code_coupon = lg.code_coupon 
		LEFT JOIN restaurant AS r on r.id = lg.restaurant_id 
		LEFT JOIN staff AS u ON u.id = eu.admin_id 
		WHERE eu.code_coupon = '".$code_coupon."'"; //die(''.$sql);
		$data = $this->db->query($sql)->row_object();
		return $data;
	}
	
    public function restaurantName()
    {
        return $this->db->query('SELECT name FROM restaurant WHERE id = '.$this->session->userdata('restaurant_id'))->row_object()->name;
    }
    
    public function getEcouponInRestaurant()
    {
        return $this->db->query('SELECT cp.title FROM ecoupon AS cp LEFT JOIN ecoupon_key AS ek ON ek.id_ecoupon = cp.id LEFT JOIN restaurant AS res ON res.id = ek.id_restaurant WHERE res.id = '.$this->session->userdata('restaurant_id').' AND cp.active = 1 AND cp.date_user > '.time())->result_object();
    }
}
